package cn.tianya.irock.security;

import cn.tianya.irock.core.DateTime;

public class FormsAuthenticationTicket {
	int version;
	boolean isPersistent;
	String name;
	String userData;
	String cookiePath;
	DateTime issueDate;
	DateTime expiration;
	
	public FormsAuthenticationTicket (int version,
									  String name,
									  DateTime issueDate,
									  DateTime expiration,
									  boolean isPersistent,
									  String userData)
	{
		this.version = version;
		this.name = name;
		this.issueDate = issueDate;
		this.expiration = expiration;
		this.isPersistent = isPersistent;
		this.userData = userData;
		this.cookiePath = "/";
	}
	
	public FormsAuthenticationTicket (int version,
									  String name,
									  DateTime issueDate,
									  DateTime expiration,
									  boolean isPersistent,
									  String userData,
									  String cookiePath)
	{
		this.version = version;
		this.name = name;
		this.issueDate = issueDate;
		this.expiration = expiration;
		this.isPersistent = isPersistent;
		this.userData = userData;
		this.cookiePath = cookiePath;
	}
	
	public FormsAuthenticationTicket (String name, boolean isPersistent, int timeout)
	{
		this.version = 1;
		this.name = name;
		this.issueDate = new DateTime();
		this.isPersistent = isPersistent;
		if (isPersistent)
			expiration = issueDate.addYears (50);
		else
			expiration = issueDate.addMinutes (timeout);
		
		this.userData = "";
		this.cookiePath = "/";
	}

	public int getVersion() {
		return version;
	}
	public boolean isPersistent() {
		return isPersistent;
	}
	public String getName() {
		return name;
	}
	public String getUserData() {
		return userData;
	}
	public String getCookiePath() {
		return cookiePath;
	}
	public DateTime getIssueDate() {
		return issueDate;
	}
	public DateTime getExpiration() {
		return expiration;
	}
	
	public boolean expired() {
		return DateTime.now().getTime() > expiration.getTime();
	}
	
}
